package com.yizhubao.app.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.weikaiyun.fragmentation.SupportFragment;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;
import com.yizhubao.app.R;
import com.yizhubao.app.utils.net.LifeCycleEvent;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import h.framework.pacy.utils.ScreenUtil;
import h.framework.pacy.utils.ToastUtil;
import io.reactivex.subjects.PublishSubject;

/**
 * 创建人 PACY
 * 创建时间 18/8/23 11:23.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述 不需要Toolbar的BaseFragment
 */

public abstract class BaseFragment extends SupportFragment implements BaseView {
    private View mContentView;

    Unbinder unbinder;

    public View notDataView;
    public View errorView;

    LoadingDialog dialog;

    //用于控制retrofit的生命周期，以便在destroy或其他状态时终止网络请求
    public PublishSubject<LifeCycleEvent> lifecycleSubject = PublishSubject.create();

    //该方法用于提供lifecycleSubject（相当于实现了IBaseView中的getLifeSubject抽象方法）。
    //方便Presenter中直接通过IBaseView获取lifecycleSubject，而不用每次都作为参数传递过去
    public PublishSubject<LifeCycleEvent> getLifeSubject() {
        return lifecycleSubject;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(setLayoutResourceID(), container, false);
        unbinder = ButterKnife.bind(this, mContentView);
        getErrorView();
        getNotDataView();
        initView();
        initData();
        return mContentView;

    }


    /**
     * 初始化view
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    public void showLoading(String title){
        dialog = new LoadingDialog(getActivity());
        dialog.setLoadingText(title);//设置loading时显示的文字
        dialog.show();
    }
    public void showLoading(){
        showLoading("加载中");
    }

    /**
     * 此方法用于返回Fragment设置ContentView的布局文件资源ID
     *
     * @return 布局文件资源ID
     */
    public abstract int setLayoutResourceID();

    @Override
    public void showFailure(String msg) {
        ToastUtil.showShort(msg);
        showFinished();
    }


    @Override
    public void showFinished() {
        if(dialog!=null){
            dialog.close();
        }
    }

    /**
     * 获取网络错误view
     */
    public View getErrorView() {
        errorView = getLayoutInflater().inflate(R.layout.network_error_view, null, false);
        return errorView;
    }

    public View getNotDataView(){
        notDataView = getLayoutInflater().inflate(R.layout.product_empty_view, null, false);
        return notDataView;
    }



    public void startActivity(Context context, Class<?> clz) {
        startActivity(context,clz,null);
    }

    public void startActivity(Context context, Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(context, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    public void onPause() {
        lifecycleSubject.onNext(LifeCycleEvent.PAUSE);
        super.onPause();
    }

    @Override
    public void onStop() {
        lifecycleSubject.onNext(LifeCycleEvent.STOP);
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        lifecycleSubject.onNext(LifeCycleEvent.DESTROY);

//        RefWatcher refWatcher = MyApplication.getRefWatcher(getActivity());
//        if (refWatcher != null) refWatcher.watch(this);//内存泄露检测
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
